<style type="text/css">
        .layui-table-cell {
            height: auto;
            line-height: 28px;
        }
    </style>
<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">顾客管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!home_console">首页</a>
          <a><cite>顾客管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
         <div class="layui-form-item">
        <div class="layui-inline">
          <label class="layui-form-label">购买人姓名</label>
          <div class="layui-input-block">
            <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">顾客手机号</label>
          <div class="layui-input-block">
            <input type="text" name="phone" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        
        
        <div class="layui-inline">
          <button id="user-btn-search"
						class="layui-btn layuiadmin-btn-useradmin" lay-submit
						lay-filter="LAY-front-search">
						<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
					</button>
            <button id="btn-reset" class="layui-btn icon-btn"><i class="layui-icon">&#x1006;</i>重置</button>
            <% if(add) { %><button id="shop-btn-add" class="layui-btn icon-btn" ><i class="layui-icon">&#xe654;</i>添加
            </button><% } %>
        </div>
      </div>
           
        </div> 

        <table class="layui-table" id="customer-table" lay-filter="customer-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="customer-table-bar">

    <% if(update) { %><a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a><% } %>
{{# if (d.customerStatus=== 1) { }}
    <% if(delete) { %><a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a><% } %>
{{# } else if(d.customerStatus=== 0) { }}  
	<% if(back) { %><a class="layui-btn  layui-btn-normal layui-btn-xs" lay-event="back">恢复</a><% } %>
{{# } }}
</script>

<!-- 表格状态列 -->
<script type="text/html" id="cardName-tpl-state">
	{{# if (d.cardName=== '1') { }}  
		<button class="layui-btn layui-btn-xs layui-btn-normal">脸卡</button>
	{{# } else if(d.cardName=== '2') { }}  
		<button class="layui-btn layui-btn-xs layui-btn-danger">颈卡</button>
	{{# } }}
</script>
<!-- 表格状态列 -->
<script type="text/html" id="cardType-tpl-state">
	{{# if (d.cardType=== 1) { }}  
		<button class="layui-btn layui-btn-xs layui-btn-normal">次数卡</button>
	{{# } else if(d.cardType=== 2) { }}  
		<button class="layui-btn layui-btn-xs layui-btn-danger">充值卡</button>
	{{# } }}
</script>
<!-- 表格状态列 -->
<script type="text/html" id="customerSex-tpl-state">
	{{# if (d.customerSex=== 1) { }}  
		<button class="layui-btn layui-btn-xs layui-btn-normal">男</button>
	{{# } else if(d.customerSex=== 0) { }}  
		<button class="layui-btn layui-btn-xs layui-btn-danger">女</button>
	{{# } }}
</script>
<!-- 表格状态列 -->
<script type="text/html" id="customerStatus-tpl-state">
	{{# if (d.customerStatus=== 1) { }}  
		<button class="layui-btn layui-btn-xs layui-btn-normal">正常</button>
	{{# } else if(d.customerStatus=== 0) { }}  
		<button class="layui-btn layui-btn-xs layui-btn-danger">停用</button>
	{{# } }}
</script>
<script>
    layui.use(['form', 'table', 'util', 'admin', 'element'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var element = layui.element;

        form.render('select');

        // 渲染表格
        
        table.render({
            elem: '#customer-table',
            url: 'system/customer/list',
            where: {shopId: $("#shopInfo").attr("data")},
            page: true,
            cols: [[
                {type: 'numbers', title: '序号'},
                /* {field: 'customerCode', title: '顾客编号'}, */
                {field: 'customerName', title: '顾客姓名'},
                {field: 'customerPhone',title: '手机号'},
                {field: 'customerSex', templet: '#customerSex-tpl-state', title: '性别'},
                {field: 'customerBirthday',  title: '生日'},
                {field: 'customerAddress',  title: '地址'},
                {field: 'customerStatus',  title: '状态',templet: '#customerStatus-tpl-state'},
                {
                    sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {align: 'center', toolbar: '#customer-table-bar', title: '操作'}
            ]]
        });

        // 添加按钮点击事件
        $('#shop-btn-add').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(customer-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
				layer.confirm('确定要删除吗？', function() {
					layer.load(2);
					admin.req("system/customer/delete", {
						id : obj.data.id
					}, function(data) {
						layer.closeAll('loading');
						if (data.code == 1) {
							layer.msg(data.msg, {
								icon : 1
							});
							
						} else {
							layer.msg(data.msg, {
								icon : 2
							});
						}
						table.reload('customer-table', {});
					}, "post");
				});
            }else if (layEvent === 'back') { //恢复
				layer.confirm('确定要恢复客户吗？', function() {
					layer.load(2);
					admin.req("system/customer/back", {
						id : obj.data.id
					}, function(data) {
						layer.closeAll('loading');
						if (data.code == 1) {
							layer.msg(data.msg, {
								icon : 1
							});
							
						} else {
							layer.msg(data.msg, {
								icon : 2
							});
						}
						table.reload('customer-table', {});
					}, "post");
				});
            }
        });

        // 显示表单弹窗
        var showEditModel = function (data) {
            var title = data ? '修改顾客信息' : '新增顾客';
            admin.putTempData('t_customer', data);
            var str =  data ? JSON.stringify(data) : " ";
            admin.popupCenter({
            	area: '40%',
            	offset: '10%',
                title: title,
                path: "system/customer/editForm?shopId="+$("#shopInfo").attr("data"),
                data:JSON.stringify(data),
                finish: function () {
                    table.reload('customer-table', {});
                }
            });
        };

        //监听搜索
        form.on('submit(LAY-front-search)', function(data){
          var field = data.field;
          table.reload('customer-table', {where: {name: field.name, phone: field.phone}});
        });


        
     // 搜索按钮点击事件
		$('#btn-reset').click(function() {
			location.reload();
		});
    });
</script>